import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/25 15:48
 * @description 二维前缀和 + 排序
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] ma = {{5, 2}, {1, 6}};
        solution.kthLargestValue(ma, 2);
    }
    public int kthLargestValue(int[][] matrix, int k) {
        int m = matrix.length, n = matrix[0].length;
        int[][] pre = new int[m + 1][n + 1];
        List<Integer> results = new ArrayList<Integer>();
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                // 求二维的异或
                pre[i][j] = pre[i - 1][j] ^ pre[i][j - 1] ^ pre[i - 1][j - 1] ^ matrix[i - 1][j - 1];
                results.add(pre[i][j]);
            }
        }

        Collections.sort(results, (num1, num2) -> num2 - num1);
        return results.get(k - 1);
    }
}
